#include <sys/timerfd.h>
#include <iostream>
#include <string.h>
#include <errno.h>
#include <unistd.h>
int main()
{
    int fd = timerfd_create(CLOCK_MONOTONIC, 0);
    if (fd == -1)
    {
        std::cerr << "Error creating timerfd: " << strerror(errno) << std::endl;
        return -1;
    }
    itimerspec its;
    its.it_value.tv_sec = 1;
    its.it_value.tv_nsec = 0;
    its.it_interval.tv_sec = 1;
    its.it_interval.tv_nsec = 0;
    int ret = timerfd_settime(fd, 0, &its, NULL);
    if (ret == -1)
    {
        std::cerr << "Error setting timerfd: " << strerror(errno) << std::endl;
        return -1;
    }
    uint64_t exp;

    while (1)
    {
        ssize_t n = read(fd, &exp, sizeof(exp));
        if (n == -1)
        {
            std::cerr << "Error reading timerfd: " << strerror(errno) << std::endl;
            return -1;
        }
        std::cout << "expired " << exp << " times" << std::endl;
    }

    close(fd);

    return 0;
}